<?php

	namespace app\middleware\api;

    use app\model\ucenter\User as UserModel;
	use think\exception\HttpException;
    use app\model\ucenter\UserToken as UserTokenModel;
    use app\event\ucenter\User as UserEvent;

	/**
	 * 访问环境检查，是否是微信或支付宝等
	 */
	class UserLogin
	{
		public function handle($request, \Closure $next)
		{
            // token
            $token = $request->header('authorization', $request->request('token', cookie('token')??''));
//            $token = $request->header();

            if (strlen($token)>16)
            {
                $data = UserTokenModel::where('token',$token)->find();
                if (!$data)
                {
                    throw new HttpException(410003, 'token已经失效，请重新登录！');
                }
                $res = UserEvent::getInfoByToken($token);
                if ($res['code'] != 0) {
                    throw new HttpException($res['code'], '账户异常，请重新登录！');
                }
                $request->user = $res['data'];
            }
			return $next($request);
		}

	}